#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using System.Collections;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    [Gui.Design.DisplayName("My custom strategy")]
    public class MyCustomStrategy : Strategy
    {
        #region Variables
        // Wizard generated variables
        // User defined variables (add any user defined variables below)
		//<MarketPosition>
		private ArrayList _marketPositionHistory;
		//</MarketPosition>
        #endregion

        #region Constructors
        /// <summary>
        /// Constructor
        /// </summary>
        public MyCustomStrategy() 
        {
            _marketPositionHistory = new ArrayList();
        }
        #endregion Constructors
		
        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
            CalculateOnBarClose = true;
			AddInstrument();
            AddInstrument();
			
			Add("ER2 06-07", PeriodType.Minute, 1);
			
        }

        /// <summary>
        /// Called on each bar update event (incoming tick).
        /// </summary>
        protected override void OnBarUpdate()
        {
			if(ToTime(Time[0]) == 230000)
			{
				EnterLong();
			}
			if(ToTime(Time[0]) == 230500)
			{
				ExitLong();
			}
			if(ToTime(Time[0]) == 231800)
			{
				EnterLong();
			}
			if(ToTime(Time[0]) == 232000)
			{
				ExitLong();
			}
			
			if(ToTime(Time[0]) == 232100)
			{
				EnterShort();
			}
			if(ToTime(Time[0]) == 232500)
			{
				ExitLong();
			}
			
			if(ToTime(Time[0]) == 233200)
			{
				EnterLong();
			}
			if(ToTime(Time[0]) == 233600)
			{
				ExitLong();
			}
			
			if(ToTime(Time[0]) == 235000)
			{
				EnterLong();
			}
			if(ToTime(Time[0]) == 235500)
			{
				ExitLong();
			}
			if(ToTime(Time[0]) == 235800)
			{
				Print("Mod(20.33, 15):" + (Round(Mod(20.33, 15),2) == 5.33 ? "ok" : "error"));
				Print("Mod(-528, 2.333):" + (Round(Mod(-528, 2.333),2) == -0.74 ? "ok" : "error"));
				
				Print("BarsSinceEntry(0):" + (BarsSinceEntry(0) == 7 ? "ok" : "error"));
				Print("BarsSinceEntry(1):" + (BarsSinceEntry(1) == 25 ? "ok" : "error"));
				Print("BarsSinceEntry(2):" + (BarsSinceEntry(2) == 36 ? "ok" : "error"));
				Print("BarsSinceEntry(3):" + (BarsSinceEntry(3) == 39 ? "ok" : "error"));
				Print("BarsSinceEntry(4):" + (BarsSinceEntry(4) == 57 ? "ok" : "error"));
				
				Print("BarsSinceExit(0);" + (BarsSinceExit(0) == 2 ? "ok" : "error"));
				Print("BarsSinceExit(1);" + (BarsSinceExit(1) == 21 ? "ok" : "error"));
				Print("BarsSinceExit(2);" + (BarsSinceExit(2) == 32 ? "ok" : "error"));
				Print("BarsSinceExit(3);" + (BarsSinceExit(3) == 37 ? "ok" : "error"));
				Print("BarsSinceExit(4);" + (BarsSinceExit(4) == 52 ? "ok" : "error"));

				Print("BarInterval();" + (BarInterval() == 1 ? "ok" : "error"));
				//Print("BarType();" + BarType());
				Print("TotalTrades();" + (TotalTrades() == 5 ? "ok" : "error"));
				Print("CurrentDate;" + CurrentDate());
				
				Print("DateToJulian(1070401);" + DateToJulian(1070401));
				Print("DateToJulian(1070402);" + DateToJulian(1070402));
				Print("DateToJulian(1070403);" + DateToJulian(1070403));
				Print("JulianToDate(39174);" + JulianToDate(39174));
				
				Random rnd = new Random(5);
				Print("rnd(5);" + rnd.ToString());
				
				Print("Highest(Close, 20);" + (Highest(Close, 20)[0] == 1434.50 ? "ok" : "error"));
				Print("Highest(Open, 10);" + (Highest(Open, 10)[0] == 1434.50 ? "ok" : "error"));
				Print("Highest(Low, 15);" + (Highest(Low, 15)[0] == 1434.25 ? "ok" : "error"));
				
				Print("Lowest(Open, 20);" + (Lowest(Open, 20)[0] == 1433.25 ? "ok" : "error"));
				Print("Lowest(Close, 10);" + (Lowest(Close, 10)[0] == 1433.75 ? "ok" : "error"));
				Print("Lowest(Low, 15);" + (Lowest(Low, 15)[0] == 1433.75 ? "ok" : "error"));
				
				Print("Average(Close, 20);" + (Average(Close, 20)[0] == 1434.05 ? "ok" : "error"));
				Print("Average(Open, 19);" + (Round(Average(Open, 19)[0],2) == 1434.08 ? "ok" : "error"));
				Print("Average(Close, 10);" + (Average(Close, 10)[0] == 1434.1 ? "ok" : "error"));
				Print("Average(Low, 15);" + (Average(Low, 15)[0] == 1433.95 ? "ok" : "error"));
				
				
				Print("XAverage(Open, 20);" + (Round(XAverage(Open, 20)[0],2) == 1434.18 ? "ok" : "error"));
				Print("XAverage(High, 20);" + (Round(XAverage(High, 20)[0],2) == 1434.41 ? "ok" : "error"));
				Print("XAverage(Close, 10);" + (Round(XAverage(Close, 10)[0],2) == 1434.09 ? "ok" : "error"));
				Print("XAverage(Low, 15);" + (Round(XAverage(Low, 15)[0],2) == 1433.90 ? "ok" : "error"));
				
				
				Print("EL_Momentum(Low, 15);" + (EL_Momentum(Low, 15)[0] == 0.75 ? "ok" : "error"));
				Print("ELDate(2007, 04, 02);" + (ELDate(2007, 04, 02) == 1070402 ? "ok" : "error"));
				Print("AbsValue(-12.12);" + (AbsValue(-12.12)  == 12.12 ? "ok" : "error"));
				Print("Arctangent(43.34);" + (Round(Arctangent(43.34),2) == 88.68 ? "ok" : "error"));
				Print("AvgBarsLosTrade();" + (Round(AvgBarsLosTrade(),2) == 3.33 ? "ok" : "error"));
				Print("AvgBarsWinTrade();" + (AvgBarsWinTrade() == 5.0 ? "ok" : "error"));
				
				//Print("AvgEntryPrice();" + AvgEntryPrice());
				double[] list = {13.12, 15.16, 3.0, -20.3, 44};
				Print("AvgList(list);" + (Round(AvgList(list), 2) == 11.0 ? "ok" : "error"));
				double[] list1 = {18, 67, 98, 24, 65, 19};
				Print("AvgList(list1);" + (AvgList(list1) == 48.5 ? "ok" : "error"));
				
				
				//Print("Beta();" + Beta());
				Print("BigPointValue();" + (BigPointValue() == 50 ? "ok" : "error"));
				//Print("BoxSize();" + BoxSize());
				Print("EL_Category;" + (EL_Category() == 0 ? "ok" : "error"));
				Print("Ceiling(324.73);" + (Ceiling(324.73) == 325 ? "ok" : "error"));
				//Print("Commission();" + Commission());
				//Print("CommodityNumber();" + CommodityNumber());
				Print("Cosine(12.3);" + (Round(Cosine(12.3),2) == 0.98 ? "ok" : "error"));
				Print("Cotangent(12.22);" + (Round(Cotangent(12.22),2) == 4.62 ? "ok" : "error"));
				Print("CurrentBar();" + CurrentBar);
				//Print("CurrentContracts();" + CurrentContracts());
				
				//Print("CurrentEntries();" + CurrentEntries());
				//Print("DailyLimit();" + DailyLimit());
				Print("DataCompression();" + DataCompression());
				Print("DayOfMonth(1070402);" + DayOfMonth(1070402));
				Print("DayOfWeek(1070402);" + DayOfWeek(1070402));
				Print("DeliveryMonth();" + DeliveryMonth());
				Print("DeliveryYear();" + DeliveryYear());
				
				//Print("Dividend();" + Dividend());
				//Print("Dividend_Yield();" + Dividend_Yield());
				Print("EntryDate(4);" + EntryDate(4));
				//Print("EntryPrice(10);" + EntryPrice(10));
				Print("EntryTime(1);" + EntryTime(1));
				//Print("EPS();" + EPS());
				Print("ExitDate(3);" + ExitDate(3));
				//Print("ExitPrice(1);" + ExitPrice(1));
				Print("ExitTime(0);" + ExitTime(0));
				
				Print("ExpValue(34.3);" + ExpValue(34.3));
				//Print("FirstNoticeDate();" + FirstNoticeDate());
				Print("Floor(93.1);" + Floor(93.1));
				Print("FracPortion(68.3);" + FracPortion(68.3));
				
				Print("GetBackgroundColor();" + GetBackgroundColor());
				//Print("GetPlotBGColor(2);" + GetPlotBGColor(2));
				
				Print("GrossLoss();" + GrossLoss());
				Print("GrossProfit();" + GrossProfit());
				//Print("I_AvgEntryPrice();" + I_AvgEntryPrice());
				//Print("I_ClosedEquity();" + "");
				//Print("I_CurrentContracts();" + "");
				//Print("I_MarketPosition();" + "");
				//Print("I_OpenEquity();" + "");
				//Print("InitialMargin();" + "");
				Print("InStr('Abaraboku', 'rab');" + InStr("Abaraboku", "rab"));
				Print("IntPortion(25.34);" + IntPortion(25.34));
				Print("LargestLosTrade();" + LargestLosTrade());
				Print("LargestWinTrade();" + LargestWinTrade());
				
				Print("LastCalcJDate();" + LastCalcJDate());
				Print("LastCalcMMTime();" + LastCalcMMTime());
				Print("Log(67.04);" + Log(67.04));
				//Print("Margin();" + "");//No analog in Ninja (Confirmed)
				/*
				Print("EL_MarketPosition();" + EL_MarketPosition());
				Print("EL_MarketPosition(1);" + EL_MarketPosition(1));
				Print("EL_MarketPosition(2);" + EL_MarketPosition(2));
				Print("EL_MarketPosition(3);" + EL_MarketPosition(3));
				Print("EL_MarketPosition(4);" + EL_MarketPosition(4));
				Print("EL_MarketPosition(5);" + EL_MarketPosition(5));
				Print("EL_MarketPosition(6);" + EL_MarketPosition(6));
				Print("EL_MarketPosition(1000);" + EL_MarketPosition(1000));
				*/
				//Print("MaxBarsBack();" + ""); //No analog in Ninja (Confirmed)
				Print("MaxBarsForward();" + MaxBarsForward());
				Print("MaxConsecLosers();" + MaxConsecLosers());
				Print("MaxConsecWinners();" + MaxConsecWinners());

                Print("Sunday();" + Sunday());
                Print("Monday();" + Monday());
                Print("Tuesday();" + Tuesday());
                Print("Wednesday();" + Wednesday());
                Print("Thursday();" + Thursday());
                Print("Friday();" + Friday());
                Print("Saturday();" + Saturday());

                Print("Product();" + Product("2000i"));
                Print("StrToNum('123.409')" + StrToNum("123.409"));
                Print("StrToNum('-3.999')" + StrToNum("-3.999"));
                Print("StrToNum('-1')" + StrToNum("-1"));
                Print("StrToNum('+267.908')" + StrToNum("+267.908"));

                double[] maxminlist = { 23.3, 24.5, 444.23, -943.1, 0, +2, -1.23, -54 };
                Print("MaxList(int[] list);" + MaxList(maxminlist));
                Print("MaxList2(int[] list);" + MaxList2(maxminlist));
                Print("MinList(int[] list);" + MinList(maxminlist));
                Print("NthMaxList(4,maxminlist);" + NthMaxList(4, maxminlist));
                Print("SumList(int[] list);" + SumList(maxminlist));

                /*
                
				Print("MaxContracts(int num);" + "");//No analog in Ninja (ask)
				Print("MaxEntries(int num);" + "");//No analog in Ninja (ask)
				Print("MaxContractsHeld();" + "");//No analog in Ninja (ask)
				Print("MaxIDDrawdown();" + "");//No analog in Ninja (ask)
				Print("MaxPositionLoss(int num);" + "");//No analog in Ninja (ask)
				Print("MaxPositionProfit();" + "");//No analog in Ninja (ask)
				Print("MinMove();" + "");//No analog in Ninja (ask)
				Print("Month(int cDate);" + "");
				Print("NetProfit();" + "");
				Print("NumLosTrades();" + "");
				Print("NumWinTrades();" + "");
				Print("OpenPositionProfit();" + "");
				Print("PercentProfit();" + "");
				Print("Pow(double num, double exponent);" + "");
				Print("Round(double num, int precision);" + "");
				Print("Sess1StartTime();" + ""); //No analog in Ninja (ask)
				Print("Sess1EndTime();" + "");//No analog in Ninja (ask)
				Print("Sess1FirstBarTime();" + "");//No analog in Ninja (ask)
				Print("Sess2StartTime();" + "");//No analog in Ninja (ask)
				Print("Sess2EndTime();" + "");//No analog in Ninja (ask)
				Print("Sess2FirstBarTime();" + "");//No analog in Ninja (ask)
				Print("UnionSess1StartTime();" + "");//No analog in Ninja (ask)
				Print("UnionSess1EndTime();" + "");//No analog in Ninja (ask)
				Print("UnionSess1FirstBarTime();" + "");//No analog in Ninja (ask)
				Print("UnionSess2StartTime();" + "");//No analog in Ninja (ask)
				Print("UnionSess2EndTime();" + "");//No analog in Ninja (ask)
				Print("UnionSess2FirstBarTime();" + "");//No analog in Ninja (ask)
				Print("Sessions();" + "");
				Print("Sign(double num);" + "");
				Print("Sine(double num);" + "");
				Print("EL_Slippage();" + "");
				Print("Square(double num);" + "");
				Print("SquareRoot(double num);" + "");
				Print("StrLen(String str);" + "");
				Print("StrToNum(String str);" + "");
				Print("SymbolNumber();" + "");
				Print("Tangent(double num);" + "");
				Print("TickType();" + "");
				Print("TotalBarsLosTrades();" + "");
				Print("TotalBarsWinTrades();" + "");
				Print("Year();" + "");
				Print("StockSplit();" + "");
				Print("StockSplitCount();" + "");
				Print("StockSplitDate();" + "");
				Print("StockSplitTime();" + "");
				Print("Text_New();" + "");
				Print("Text_Delete();" + "");
				Print("Text_GetColor();" + "");
				Print("Text_GetDate();" + "");
				Print("Text_GetTime();" + "");
				Print("Text_GetFirst();" + "");
				Print("Text_GetNext();" + "");
				Print("Text_GetHStyle();" + "");
				Print("Text_GetVStyle();" + "");
				Print("Text_GetValue();" + "");
				Print("Text_SetColor();" + "");
				Print("Text_SetLocation();" + "");
				Print("Text_SetString();" + "");
				Print("Text_SetStyle();" + "");
				Print("TL_New();" + "");
				Print("TL_GetAlert();" + "");
				Print("TL_GetBeginDate();" + "");
				Print("TL_GetEndDate();" + "");
				Print("TL_GetBeginTime();" + "");
				Print("TL_GetEndTime();" + "");
				Print("TL_GetBeginValue();" + "");
				Print("TL_GetEndValue();" + "");
				Print("TL_GetColor();" + "");
				Print("TL_GetFirst();" + "");
				Print("TL_GetNext();" + "");
				Print("TL_GetSize();" + "");
				Print("TL_SetSize();" + "");
				Print("TL_GetStyle();" + "");
				Print("TL_SetStyle();" + "");
				Print("TL_GetValue();" + "");
				Print("TL_SetAlert();" + "");
				Print("TL_SetBegin();" + "");
				Print("TL_SetEnd();" + "");
				Print("TL_SetColor();" + "");
				Print("TL_SetExtLeft();" + "");
				Print("TL_SetExtRight();" + "");
				Print("deriving();" + "");
				*/
			}
			
			
        }

		//<MarketPosition>
        ///The market position (1 = long, -1 = short, 0 = flat) of the specified position.
        ///Syntax: MarketPosition(Num)
        ///Num: number of positions ago
        public int EL_MarketPosition(int num)
		{
			
			int i, tradeNumber = 0;
			DataSeries ds = (DataSeries)_marketPositionHistory[BarsInProgress];
			for (i = 0; i < CurrentBar; i++)
			{
				if(ds[i] != tradeNumber && ds[i] != 0)
				{
					tradeNumber = (int)ds[i];
					if (--num == 0)
						break;
				}
				
			}
			if(num != 0)
				return 0;
			return Math.Sign(ds[i]);
		}
        ///The market position (1 = long, -1 = short, 0 = flat) of the specified position.
		protected void UpdateELData()
		{
			DataSeries ds = (DataSeries)_marketPositionHistory[BarsInProgress];
			if(Positions[BarsInProgress].MarketPosition == MarketPosition.Flat)
				ds.Set(0);
			else if(Positions[BarsInProgress].MarketPosition == MarketPosition.Long)
				ds.Set(Performance.AllTrades.Count);
			else if(Positions[BarsInProgress].MarketPosition == MarketPosition.Short)
				ds.Set(-Performance.AllTrades.Count);
		}
		
		protected void AddInstrument()
		{
			_marketPositionHistory.Add(new DataSeries(this));		
		}
		//</MarketPosition>
		

        #region Properties
        #endregion
    }
}
